<?php
/**
 * Created by PhpStorm.
 * User: HP_USER
 * Date: 2019/3/14
 * Time: 15:24
 */
namespace app\admin\controller;

use app\AdminData\ProxyShopData;
use app\common\Common;
use think\Db;

class ProxyShop extends Base
{

    public function checkProxyShop($img, $address, $business_license, $proxy_name)
    {
        $count = count($img);
        if ($count > 10) {
            return ['code' => $this->f_code, 'msg' => '资质图片不能超过10张'];
        }
        $len = mb_strlen($address);
        if ($len > 100) {
            return ['code' => $this->f_code, 'msg' => '地址长度过长,请控制在100字内'];
        }
        $len = strlen($business_license);
        if ($len > 18 || $len < 15) {
            return ['code' => $this->f_code, 'msg' => '营业执照错误'];
        }
        $len = mb_strlen($proxy_name);
        if ($len > 40) {
            return ['code' => $this->f_code, 'msg' => '名称长度不能超过40个字'];
        }
        return ['code' => $this->t_code];
    }

    /**
     * 添加渠道商户
     * @param $name
     * @param $pwd
     * @param $img
     * @param $address
     * @param $business_license
     * @param $proxy_name
     * @return \think\response\Json
     */
    public function addProxyShop($name, $pwd, array $img, $address, $business_license, $proxy_name)
    {
        $len = mb_strlen($name);
        if ($len < 4 || $len > 18) {
            return json(['code' => $this->f_code, 'msg' => '账号长度4-18位']);
        }
        $len = mb_strlen($pwd);
        if ($len != 32) {
            return json(['code' => $this->f_code, 'msg' => '密码填写错误']);
        }
        $res = $this->checkProxyShop($img, $address, $business_license, $proxy_name);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFields('drh_proxy_shop_account', 'account_name', $name) == $this->t_code) {
            return json(['code' => $this->f_code, 'msg' => '账号以存在']);
        }
        $time = time();
        $pd = new ProxyShopData();
        Db::startTrans();
        $id = $pd->addProxyShop($name, $pwd, $time);
        if ($id) {
            if ($this->addProxyShopInfo($id, $img, $address, $business_license, $proxy_name, $pd)) {
                Db::commit();
                return json(['code' => $this->t_code, 'msg' => '创建成功', 'data' => $id]);
            }
        }
        Db::rollback();
        return json(['code' => $this->f_code, 'msg' => '网络连接错误,请稍后在试!']);
    }

    public function addProxyShopInfo($user_id, $img, $address, $business_license, $proxy_name, ProxyShopData $pd)
    {
        $res = $pd->addProxyShopInfo($user_id, $img, $address, $business_license, $proxy_name);
        if ($res) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 渠道商户登陆
     * @param $name
     * @param $pwd
     * @return \think\response\Json
     */
    public function loginProxyShop($name, $pwd)
    {
        $tel = Common::getTableValue('drh_proxy_shop_account', 'id', ['account_name', 'account_pwd'], [$name, $pwd]);
        if (!empty($tel)) {
            return json(['code' => $this->f_code, 'msg' => '账号密码错误']);
        }
        return json(['code' => $this->t_code, 'msg' => '登陆成功!', 'data' => $tel]);
    }
}